package com.softer.chengxin.score.dao.provider;

import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.score.entity.ScoreRecord;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;

/**
 * @author Gong Zhiyang
 * @date 2018/10/12
 */
public class ScoreManagerDaoProvider {
    /**
     * 获取企业信用等级数量
     *
     * @param map 查询实体
     * @return 数量
     */
    public String getScoreRecordNum(HashMap<String, ScoreRecord> map) {
        StringBuffer sql = new StringBuffer(20);
        ScoreRecord scoreRecord = map.get("scoreRecord");
        sql.append("SELECT COUNT(*) FROM score_record a LEFT JOIN company_base_info b ON a.poguid=b.id");
        sql.append(" WHERE a.poguid=#{scoreRecord.poguid}");
        commonSql(sql, scoreRecord);
        return sql.toString();
    }

    /**
     * 获取企业信用等级
     *
     * @param map 查询实体
     * @return 数量
     */
    public String getScoreRecordList(HashMap<String, Object> map) {
        StringBuffer sql = new StringBuffer(20);
        Page page = (Page) map.get("page");
        ScoreRecord scoreRecord = (ScoreRecord) map.get("scoreRecord");
        sql.append("SELECT a.jc_01 as jc01,a.jc_02 as jc02,a.jc_03 as jc03,a.jc_04 as jc04,a.jc_05 as jc05,a.jc_06 as jc06,a.jc_07 as jc07,");
        sql.append("a.yl_01 as yl01,a.yl_02 as yl02,a.yl_03 as yl03,a.yl_04 as yl04,a.yl_05 as yl05,a.yl_06 as yl06,a.yl_07 as yl07,a.yl_08 as yl08,");
        sql.append("a.fm_01 as fm01,a.fm_02 as fm02,a.fm_03 as fm03,a.fm_04 as fm04,a.fm_05 as fm05,a.fm_06 as fm06,a.fm_07 as fm07,");
        sql.append("a.all_score as allScore,a.poguid as poguid,a.dj,a.`year`,b.`name` as qyName,b.business_license_no as businessLicenseNo");
        sql.append(" FROM score_record a LEFT JOIN company_base_info b ON a.poguid=b.id");
        sql.append(" WHERE a.poguid=#{scoreRecord.poguid}");
        commonSql(sql, scoreRecord);
        sql.append(" LIMIT ");
        sql.append(page.getBeginIndex()).append(",").append(page.getLimit());
        return sql.toString();
    }

    private void commonSql(StringBuffer sql, ScoreRecord scoreRecord) {
        if (StringUtils.isNotBlank(scoreRecord.getDj())) {
            sql.append(" AND a.dj=#{scoreRecord.dj} ");
        }
        if (StringUtils.isNotBlank(scoreRecord.getYear())) {
            sql.append(" AND a.`year`=#{scoreRecord.year}");
        }
    }
}
